package com.fsnbigdata.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fsnbigdata.domain.*;
import com.fsnbigdata.handle.MyInterceptor;
import com.fsnbigdata.web.mixin.*;
import io.springlets.http.converter.json.BindingResultSerializer;
import io.springlets.http.converter.json.FieldErrorSerializer;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.roo.addon.web.mvc.controller.annotations.config.RooWebMvcConfiguration;
import io.tracee.binding.springmvc.TraceeInterceptor;
import java.lang.Override;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

/**
 * = WebMvcConfiguration
 *
 * TODO Auto-generated class documentation
 *
 */
@RooWebMvcConfiguration
@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

    /**
     * TODO Auto-generated method documentation
     *
     * @return LocalValidatorFactoryBean
     */
    @Primary
    @Bean
    public LocalValidatorFactoryBean validator() {
        return new LocalValidatorFactoryBean();
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return LocaleResolver
     */
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
        return localeResolver;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return LocaleChangeInterceptor
     */
    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
        localeChangeInterceptor.setParamName("lang");
        return localeChangeInterceptor;
    }
//    @Bean
//    public HandlerInterceptor getMyInterceptor(){
//        return new MyInterceptor();
//    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
        registry.addInterceptor(new TraceeInterceptor());
//        registry.addInterceptor(getMyInterceptor()).addPathPatterns("/api/**") ; // 匹配路径
    }

    @Override
    public void configureMessageConverters(
            List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper mp = new ObjectMapper();
        mp.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

        SimpleModule sm = new SimpleModule();

        // Mixin registration
        //setMixInAnnotation(Organism.class, IgnoreOrganismJsonMixin.class);
        sm.setMixInAnnotation(Distribution.class, IgnoreOrganismJsonMixin.class);
        sm.setMixInAnnotation(OrganismSpecimen.class, IgnoreOrganismJsonMixin.class);
        sm.setMixInAnnotation(OrganismDocument.class, IgnoreOrganismJsonMixin.class);
        sm.setMixInAnnotation(OrganismImage.class, IgnoreOrganismJsonMixin.class);
        sm.setMixInAnnotation(OrganismVideo.class, IgnoreOrganismJsonMixin.class);
        sm.setMixInAnnotation(Taxonomy.class, IgnoreOrganismJsonMixin.class);
        sm.setMixInAnnotation(ConservationStatus.class, IgnoreOrganismJsonMixin.class);
        sm.setMixInAnnotation(Germplasm.class, IgnoreOrganismJsonMixin.class);

        //setMixInAnnotation(Specimen.class, SpecimenJsonMixin.class);
        sm.setMixInAnnotation(SpecimenImage.class, IgnoreSpecimenJsonMixin.class);
        sm.setMixInAnnotation(SpecimenDocument.class, IgnoreSpecimenJsonMixin.class);
        sm.setMixInAnnotation(SpecimenVideo.class, IgnoreSpecimenJsonMixin.class);

        sm.setMixInAnnotation(ParkDistrict.class, IgnoreParkJsonMixin.class);
        sm.setMixInAnnotation(ParkDistrictOrganism.class, IgnoreParkDistrictParkJsonMixin.class);
        sm.setMixInAnnotation(ParkDistrictOrganismImage.class, IgnoreParkDistrictOrganismJsonMixin.class);

        sm.setMixInAnnotation(EcosystemImage.class, IgnoreEcosystemJsonMixin.class);

        sm.setMixInAnnotation(EndangeredSpeciesImage.class, IgnoreEndangeredSpeciesJsonMixin.class);

        sm.setMixInAnnotation(AnimalConservationLocation.class, IgnoreAnimalConservationJsonMixin.class);

        sm.setMixInAnnotation(Introduction.class, IgnorePlantConservationJsonMixin.class);
        sm.setMixInAnnotation(Planting.class, IgnorePlantConservationJsonMixin.class);
        sm.setMixInAnnotation(PhenologyRecord.class, IgnorePlantConservationJsonMixin.class);
        sm.setMixInAnnotation(PropagationCultivation.class, IgnorePlantConservationJsonMixin.class);
        sm.setMixInAnnotation(PlantingImage.class, IgnorePlantingJsonMixin.class);

        sm.setMixInAnnotation(VegetationImage.class, IgnoreVegetationJsonMixin.class);
        sm.setMixInAnnotation(VegetationDistribution.class, IgnoreVegetationJsonMixin.class);
        sm.setMixInAnnotation(ListQuadrat.class, IgnoreVegetationJsonMixin.class);
        sm.setMixInAnnotation(ListQuadratItem.class, IgnoreListQuadratJsonMixin.class);

        sm.setMixInAnnotation(ProductEnterprise.class, IgnoreProductJsonMixin.class);
        sm.setMixInAnnotation(ProductImage.class, IgnoreProductJsonMixin.class);
        sm.setMixInAnnotation(ProductOrganism.class, IgnoreProductJsonMixin.class);

        sm.setMixInAnnotation(Entrepreneur.class, IgnoreEnterpriseJsonMixin.class);
        sm.setMixInAnnotation(EnterpriseProduct.class, IgnoreEnterpriseJsonMixin.class);
        sm.setMixInAnnotation(EnterpriseEcosystem.class, IgnoreEnterpriseJsonMixin.class);
        sm.setMixInAnnotation(EnterpriseImage.class, IgnoreEnterpriseJsonMixin.class);

        sm.addSerializer(BindingResult.class, new BindingResultSerializer());
        sm.addSerializer(FieldError.class, new FieldErrorSerializer());

        mp.registerModule(sm);
        mp.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

        converter.setObjectMapper(mp);
        converters.add(converter);
    }

    public void simpleMoudle(){

    }
}
